package com.spzx.order;

import com.rabbitmq.client.Channel;
import com.spzx.common.core.utils.StringUtils;
import com.spzx.common.rabbit.constant.MqConst;
import com.spzx.order.service.IOrderInfoService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Description:
 *
 * @author Yanxinhao on 2024/12/30
 */
@Component
@Slf4j
public class OrderReceiver {

    @Autowired
    private IOrderInfoService orderInfoService;

    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_PAYMENT_PAY),
            value = @Queue(value = MqConst.QUEUE_PAYMENT_PAY,durable = "true"),
            key = MqConst.ROUTING_PAYMENT_PAY
    ))
    public void updateOrderStatus(Message message, String orderNo, Channel channel){
        //日志输出
        if (StringUtils.isNotEmpty(orderNo)){
            log.info("准备将订单号为：{}的订单状态修改为已支付",orderNo);
            //修改订单的支付状态
            orderInfoService.updateOrderStatus(orderNo);
        }
        //手动应答
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}